package class71To80;

import java.io.*;
import java.util.Arrays;

public class TreeFormDp {
    public static int MAX_N = 6001;

    public static int[] nums = new int[MAX_N];
    public static boolean[] boss = new boolean[MAX_N];

    public static int[] no = new int[MAX_N];

    public static int[] yes = new int[MAX_N];

    public static int n, h;

    public static int[] head = new int[MAX_N];
    public static int[] next = new int[2 * MAX_N + 1];
    public static int[] to = new int[2 * MAX_N + 1];
    public static int cnt;

    public static void build(int n) {
        Arrays.fill(head, 0, n + 1, 0);
        Arrays.fill(boss, 0, n + 1, true);
        cnt = 1;
    }

    public static void addEdge(int u, int v) {
        next[cnt] = head[u];
        to[cnt] = v;
        head[u] = cnt++;
    }


    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(br);
        PrintWriter out = new PrintWriter(System.out);
        while (in.nextToken() != StreamTokenizer.TT_EOF) {
            n = (int) in.nval;
            build(n);
            for (int i = 1; i <= n; i++) {
                in.nextToken();
                nums[i] = (int) in.nval;
            }
            for (int i = 1, father, son; i < n; i++) {
                in.nextToken();
                son = (int) in.nval;
                in.nextToken();
                father = (int) in.nval;
                addEdge(father, son);
                boss[son] = false;
            }
            for (int i = 1; i <= n; i++) {
                if (boss[i]) {
                    h = i;
                    break;
                }
            }
            f(h);

            out.println(Math.max(no[h], yes[h]));
        }
        out.flush();
        out.close();
        br.close();
    }

    public static void f(int u) {
        no[u] = 0;
        yes[u] = nums[u];
        for (int ei = head[u], v; ei > 0; ei = next[ei]) {
            v = to[ei];
            f(v);
            no[u] += Math.max(no[v], yes[v]);
            yes[u] += no[v];
        }
    }
}
